package ui;

import java.awt.Button;
import java.awt.Rectangle;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import util.Local;
import constants.CurrentConfigConstants;
import constants.DefaultConfigConstants;

public class LoginWindow extends JPanel{
	
	UIManager foo = null;

	/**
	 * run
	 */
	public LoginWindow(UIManager foo){
		this.foo = foo;
			
		setControl();
		setContentPanel();
		setListener();

	}
	
	///
	/// Control
	///
	JLabel lb_name = new JLabel();
	JLabel lb_port = new JLabel();
	
	JComboBox cb_name = new JComboBox();
	JComboBox cb_port = new JComboBox();
	
	JLabel lb_about = new JLabel();
	
	Button btn_start = new Button();
	Button btn_close = new Button();
	Button btn_about = new Button();
	
	
	/**
	 * 设置控件
	 * @throws Exception
	 */
	private void setControl() {
		
		lb_name.setText("Name:");
		lb_port.setText("Port:");
		lb_about.setText(DefaultConfigConstants.NINTALK);
		lb_about.setVisible(false);
		
		cb_name.setEditable(true);
		cb_name.addItem(CurrentConfigConstants.CLIENT_CURRENT_NAME);
		
		cb_port.setEditable(true);
		cb_port.addItem(CurrentConfigConstants.CLIENT_CURRENT_PORT);
		
		
		btn_start.setLabel("Start");
		btn_close.setLabel("Close");
		btn_about.setLabel("About");
		
	}
	
	/**
	 * 设置主面板
	 * @throws Exception
	 */
	private void setContentPanel() {
		
		
		
		// 添加控件到面板
		this.add(lb_name, null);
		this.add(lb_port, null);
		this.add(lb_about, null);
		this.add(cb_name, null);
		this.add(cb_port, null);
		this.add(btn_start, null);
		this.add(btn_close, null);
		this.add(btn_about, null);
		
		// 布局控制
		// X, Y , Width, Height
		this.setLayout(null);
		this.setBounds(5,5,410,420);
		lb_name.setBounds(new Rectangle(5, 10, 95, 20));
		lb_port.setBounds(new Rectangle(5, 70, 95, 20));
		cb_name.setBounds(new Rectangle(105, 10, 130, 20));
		cb_port.setBounds(new Rectangle(105, 70, 130, 20));
		btn_start.setBounds(new Rectangle(200, 130, 80, 40));
		btn_close.setBounds(new Rectangle(5, 130, 80, 40));
		btn_about.setBounds(new Rectangle(105, 130, 80, 40));
		lb_about.setBounds(new Rectangle(5, 300, 400, 50));
	}
	
	/**
	 * 设置监听器
	 * @throws Exception
	 */

	private void setListener() {
		// btn_login 点击事件
		btn_start.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				saveConfig();
				foo.loginWindow.setVisible(false);
				foo.mainWindow.setVisible(true);
			}
		});
		// btn_close 点击事件
		btn_close.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				System.exit(0);
			}
		});
		// btn_about 点击事件
		btn_about.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				lb_about.setVisible(true);
				btn_about.enable(false);
			}
		});
	}
	
	
	/**
	 * 保存至偏好设定
	 */
	private void saveConfig() {
		String name = cb_name.getSelectedItem().toString();
		String port = cb_port.getSelectedItem().toString();
		
		CurrentConfigConstants.CLIENT_CURRENT_NAME = name;
		CurrentConfigConstants.CLIENT_CURRENT_PORT = port;
	}
}
